/*
 * @Descripttion: 
 * @Author: zhangchong zc16607@gmail.com
 * @Date: 2023-03-02 18:14:25
 * @LastEditors: zhangchong zc16607@gmail.com
 * @LastEditTime: 2024-02-07 14:13:47
 */
const commonRes = require('../utils/commonRes')
const commonNasReq = require('../utils/commonNasReq').commonReq
const { getKey, setKey } = require('../utils/fileDb')
const { sm4Encrypt, sm4Decrypt } = require('../utils/index')

class ConfigManage {

  static async getNtconfig(req, res) {
    let data = getKey('nastool')
    commonRes(res, data)
  }

  static async setNtconfig(req, res) {
    let data = req.body
    setKey('nastool', data)
    commonRes(res, null, { message: '操作成功' })
  }

  static async asyncCookie(req, res) {
    const data = await commonNasReq('/site/list', '')
    let currentData = getKey('siteRes')
    const newCurrentData = currentData.map(item => {
      let siteUrl = item.plates[0].purl
      // 获取url中的域名
      let domain = siteUrl.match(/^(https?\:)\/\/(([^:\/?#]*)(?:\:([0-9]+))?)([\/]{0,1}[^?#]*)(\?[^#]*|)(#.*|)$/)[2]
      let cookie = data.data.sites.filter(item => item.signurl.includes(domain))[0].cookie
      return { ...item, cookie: sm4Encrypt(cookie) }
    })
    setKey('siteRes', newCurrentData)

    commonRes(res, null, { message: '同步成功' })
  }
}

module.exports = ConfigManage